const hre = require("hardhat");

async function main() {
  const contractAddress = "0x5C32Fd79c758E200F8E7E71d5d37469daB239811";
  const tokenAddress = "0x7E12d6527591Ece7FDD88319c0F04AEB487D29AB";
  const userAddress = "0x694B96b770f607Da56289824fcD18818cd479F29";
  
  console.log("Checking user token balances...");
  console.log("User address:", userAddress);
  console.log("Token address:", tokenAddress);
  console.log("LaunchPad address:", contractAddress);
  
  try {
    // 获取ERC20 token合约
    const ERC20_ABI = [
      "function balanceOf(address owner) view returns (uint256)",
      "function allowance(address owner, address spender) view returns (uint256)"
    ];
    
    const tokenContract = new hre.ethers.Contract(tokenAddress, ERC20_ABI, hre.ethers.provider);
    
    // 检查用户的代币余额
    const userBalance = await tokenContract.balanceOf(userAddress);
    console.log("User token balance:", hre.ethers.formatEther(userBalance));
    
    // 检查用户给LaunchPad的授权额度
    const allowance = await tokenContract.allowance(userAddress, contractAddress);
    console.log("User allowance to LaunchPad:", hre.ethers.formatEther(allowance));
    
    // 检查LaunchPad合约中记录的用户余额
    const BSCLaunchPad = await hre.ethers.getContractFactory("BSCLaunchPad");
    const launchContract = BSCLaunchPad.attach(contractAddress);
    
    // 尝试调用getUserTokenBalance函数
    try {
      const userTokenBalance = await launchContract.getUserTokenBalance(1, userAddress);
      console.log("LaunchPad recorded balance for user:", hre.ethers.formatEther(userTokenBalance));
    } catch (error) {
      console.log("Could not get user token balance from contract:", error.message);
    }
    
  } catch (error) {
    console.error("Error:", error.message);
  }
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
